#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;

int N,M;
queue<int> q;
int dis[maxn],in[maxn];
int h[maxn],ne[maxn],e[maxn],idx;
void add(int a,int b){
    e[++idx] = b,ne[idx] = h[a],h[a] = idx;
}

int solve(){
    for(int i = 1;i<=N;i++) if(in[i] == 0) q.push(i);
    while(q.size()){
        int u = q.front();q.pop();
        for(int i = h[u];i;i = ne[i]){
            int v = e[i];
            dis[v] = max(dis[v],dis[u] + 1);
            if(--in[v] == 0){
                q.push(v);
            }
        }
    }
    int ans = 0;
    for(int i = 1;i<=N;i++) ans = max(ans,dis[i]);
    return ans;
}
int main(){
    // debug;
    ios;
    cin>>N>>M;
    for(int i = 1;i<=M;i++){
        int x,y;cin>>x>>y;
        add(x,y);
        in[y]++;
    }
    cout<<solve()<<'\n';

    
    return 0;
}
